<meta charset="utf-8"/>
<div class="row">
  <div class="col-xs-12">
    <div class="row">
      <div class="col-xs-12">
        <div class="clearfix" style="height:42px;">
          <table class="no-border" style="display:inline">
            <tr>
              <td permission='partner_btn_listData' style="padding: 0px 6px;display:none">
                登录账号
              </td>
              <td permission='partner_btn_listData' style="display:none">
                <div class="input-group">
                  <input type="text" id="key_user_name" class="form-control search-query" placeholder="登录账号" style="width:140px"/>
                </div>
              </td>
              <!--<td permission='partner_btn_listData' style="padding: 0px 6px;display:none">
                真实姓名
              </td>
              <td permission='partner_btn_listData'>
                <div class="input-group">
                  <input type="text" id="key_real_name" class="form-control search-query" placeholder="真实姓名" style="width:140px"/>
                </div>
              </td>-->
              <td permission='partner_btn_listData' style="padding: 0px 6px;display:none">
                身份证号码
              </td>
              <td permission='partner_btn_listData' style="display:none">
                <div class="input-group">
                  <input type="text" id="key_id_card" class="form-control search-query" placeholder="身份证号码" style="width:140px"/>
                </div>
              </td>
              <td permission='partner_btn_listData' class="hidden-480" style="padding: 0px 6px;display:none">
                认证状态
              </td>
              <td permission='partner_btn_listData' style="display:none">
                <div class="input-group">
                  <span class="input-group-btn">
                    <button data-toggle="dropdown" class="btn btn-primary btn-white dropdown-toggle col-sm-12" style="margin-left:0.01em">
                      <span id="searchDropdownauth_status">全部状态</span>
                      <i class="ace-icon fa fa-angle-down"></i>
                    </button>
                    <ul class="dropdown-menu col-sm-12" style="margin-left:-1px;width:99px;min-width:99px;">
                      <li>
                        <a href="javascript:winFn.dropdown('#key_auth_status','','#searchDropdownauth_status','全部状态');">全部状态</a>
                      </li>
                      <li>
                        <a href="javascript:winFn.dropdown('#key_auth_status','1','#searchDropdownauth_status','待认证');">待认证</a>
                      </li>
                      <li>
                        <a href="javascript:winFn.dropdown('#key_auth_status','2','#searchDropdownauth_status','已通过');">已通过</a>
                      </li>
                      <li>
                        <a href="javascript:winFn.dropdown('#key_auth_status','3','#searchDropdownauth_status','未通过');">未通过</a>
                      </li>
                    </ul>
                    <input id="key_auth_status" type="hidden"/>
                  </span>
                  <span class="input-group-btn">
                    <button type="button" class="btn btn-inverse btn-white" id="btnSearch">
                      <span class="ace-icon fa fa-search bigger-110"></span>
                      搜索
                    </button>
                  </span>
                </div>
              </td>
              <!--<td permission='partner_btn_row_getAllotRole' style="display:none" class="hidden-480">
                <div class="input-group">
                  <span class="input-group-btn">
                    <button type="button" class="btn btn-inverse btn-white" id="btnAllotRole">
                      <span class="fa fa-link green bigger-110"></span>
                      <span style="color:#00a6ac">角色</span>
                    </button>
                  </span>
                </div>
              </td>-->
            </tr>
          </table>
          <div class="hidden-480 pull-right tableTools-container" permission='partner_btn_listData' style="display:none"></div>
        </div>
        <table id="tableListPartner" class="table table-striped table-bordered table-hover"></table>
      </div>
    </div>
  </div>
</div>
<div id="div_role_data" style="display:none;padding:2px 0px;"></div>
<div id="divKeyColumns" style="display:none;padding:1px 0px;"></div>
<div id="div_own_menu" style="display:none;padding:2px 2px 2px 0px">
  <form class="form-horizontal">
    <div class="col-sm-12">
      <ul id="treeOwnMenu" class="ztree"></ul>
    </div>
  </form>
</div>
<div style="display:none;">
  <input type="hidden" id="partner_btn_listData"/>
  <input type="hidden" id="partner_row_edit"/>
  <input type="hidden" id="partner_row_queryById"/>
  <input type="hidden" id="partner_btn_row_getAllotRole"/>
  <input type="hidden" id="partner_btn_row_saveAllotRole"/>
  <input type="hidden" id="partner_row_getOwnMenu"/>
  <input type="hidden" id="partner_row_saveOwnMenu"/>
  <input type="hidden" id="partner_row_protocol"/>
</div>
<div id="div_audit" style="display:none;padding-top:2px;margin-bottom:-20px;">
  <form class="form-horizontal" id="form_partner_edit" role="form">
    <label class="col-sm-3 control-label no-padding-right"><div class="hr-4"></div>选择状态</label>
    <div class="col-sm-7">
      <div class="hr-4"></div>
      <div class="input-group">
      <span class="input-group-btn">
        <button data-toggle="dropdown" class="btn btn-primary btn-white dropdown-toggle col-sm-12" style="margin-left:0.01em">
          <span id="dropdown_auth_status">认证状态</span>
          <i class="ace-icon fa fa-angle-down"></i>
        </button>
        <ul class="dropdown-menu col-sm-12" style="margin-left:-1px;width:226px;min-width:226px;">
          <li>
            <a href="javascript:winFn.dropdown('#auth_status','','#dropdown_auth_status','认证状态');">认证状态</a>
          </li>
          <li>
            <a href="javascript:winFn.dropdown('#auth_status','1','#dropdown_auth_status','待认证');">待认证</a>
          </li>
          <li>
            <a href="javascript:winFn.dropdown('#auth_status','2','#dropdown_auth_status','已通过');">已通过</a>
          </li>
          <li>
            <a href="javascript:winFn.dropdown('#auth_status','3','#dropdown_auth_status','未通过');">未通过</a>
          </li>
        </ul>
        <input id="auth_status" type="hidden"/>
      </span>
      </div>
    </div>
    <label class="col-sm-3 control-label no-padding-right" for="details"><div class="hr-4"></div>认证消息</label>
    <div class="col-sm-7">
      <div class="hr-4"></div>
      <textarea type="text" id="details" placeholder="认证消息,提示认证信息" class="form-control" maxlength="256" style="height:150px;"></textarea>
    </div>
  </form>
</div>
<div id="div_chosen_select_inviter" style="display:none;padding-top:2px">
  <select class="chosen-select" id="form_select_inviter" data-placeholder="请选择邀请人……">
    <option value=""></option>
    <option value="">请输入关键字搜索</option>
  </select>
</div>
<div id="div_chosen_select_referrer" style="display:none;padding-top:2px">
  <select class="chosen-select" id="form_select_referrer" data-placeholder="请选择推荐人……">
    <option value=""></option>
    <option value="">请输入关键字搜索</option>
  </select>
</div>
<script type="text/javascript">
  var scripts = [null, null];
  $('.page-content-area').ace_ajax('loadScripts', scripts, function(){
    var tableDom = '#tableListPartner';
    $(function(){
      var urlRoute = '/partner/';/*请求controller层的url*/
      var getList = urlRoute + 'listData';/*获取数据列表*/
      var urlEdit = urlRoute + 'edit';/*审核*/
      var urlGetAllotRole = urlRoute + 'getAllotRole';/*获取角色信息*/
      var urlSaveAllotRole = urlRoute + 'saveAllotRole';/*保存分配角色*/
      var urlGetOwnMenu = urlRoute + 'getOwnMenu';/*获取菜单用于分配私有菜单*/
      var urlSaveOwnMenu = urlRoute + 'saveOwnMenu';/*保存私有菜单*/
      var urlEditProtocol = urlRoute + 'editProtocol';/*签约|解约操作*/
      var urlUserInviter = urlRoute + 'getUserInviter';/*获取邀请人列表*/
      var urlUserReferrer = urlRoute + 'getUserReferrer';/*获取推荐人列表*/
      var saveUserInviter = urlRoute + 'addUserInviter';/*保存邀请人 */
      var saveUserReferrer = urlRoute + 'addUserReferrer';/*保存推荐人*/
      var thisTable = pageDataTable.initDataTable({
        tableDom : tableDom,
        sAjaxSource : getList,
        fnServerParams : function(params){
          params.push(
            {"name": "real_name","value": $("#key_real_name").val()},
            {"name": "user_name", "value": $("#key_user_name").val()},
            {"name": "id_card", "value": $("#key_id_card").val()},
            {"name": "type", "value": 2},
            {"name": "auth_status", "value": $("#key_auth_status").val()});
        },
        aoColumns : [
          {
            bSortable : false,
            mData : "user_id",
            sWidth : "38px",
            sClass : "center",
            visible : true,/*若不用到时设为false即可,请勿删除否则有bug*/
            sTitle : '<label title="全选|不选"><input type="checkbox" class="ace" /><span class="lbl"></span></label>'
          },
          {
            mData : "user_name",
            sTitle : "登录账号",
            sWidth: "94px"
          },
          {
            mData : "referrer_name",
            sTitle : "推荐人账号",
            sWidth: "94px"
          },
          {
            mData : "parentUser",
            sTitle : "邀请人账号",
            sWidth: "94px"
          },
          {
            mData : "type",
            sTitle : "代理类型",
            sWidth: "80px",
            render : function(value){
              if(value == 1){
                return '省级代理';/*宽度width: '121px',*/
              }else if(value == 2){
                return '市级代理';
              }else if(value == 3){
                return '区级代理';
              }
              return '';
            }
          },
          {
            mData : "country_name",
            sTitle : "所属国家",
            sWidth: "80px"
          },
          {
            mData : "county_id",
            title : "代理区域",
            render : function(value,type,row,meta){
              return row.province_name + row.city_name + (row.area_name ? '，<span style="color:#eea236" title="特殊区域代理">'+row.area_name+'</span>' : '');
            }
          },
          {
            mData : "real_name",
            sTitle : "真实姓名",
            sWidth: "10%"
          },
          {
            mData : "id_card",
            sTitle : "身份证号码",
            sWidth: "147px",
            render : function(value,type,row,meta){
              if(value != null && value.length > 0){
                return "<a href='javascript:;' style='outline:none;text-decoration: none;' title='身份证号码'>"+value+"</a>";
              }else{
                return "";
              }
            }
          },
          {
            mData : "add_date",
            sTitle : "认证时间",
            sWidth: "129px",
            render : function(value){
              if(value){
                return winFn.formatDate(value,true);/*宽度width: '121px',*/
              }else{
                return "";
              }
            }
          },
          {
            mData : "auth_status",
            sTitle : "认证状态",
            sWidth: "70px",
            render : function(value,type,row,meta){
              if(value === 1){
                return '待认证';
              }else if(value === 2){
                return '已通过';
              }else if(value === 3){
                return '未通过';
              }else{
                return "";
              }
            }
          },
          {
            mData : "_user_id_",
            sTitle : "<label style='color:#000;margin-left:6px;' title='单行操作'>操作选项</label>",
            bSortable : false,
            visible : true,/*若不用到时设为false即可,请勿删除否则有bug*/
            sWidth: "128px"
          }
        ],
        columnDefs : [
          {
            targets : 0,//指定的列
            render : function(value, type, row, meta){
              return '<label title="选择|取消"><input type="checkbox" name="user_id" value="' + value + '" style="cursor:pointer;text-decoration:none;outline:none;"/><span class="lbl"></span></label>';
            }
          },
          {
            targets : -1,
            render: function(value, type, row, meta){
              return "<a href='javascript:thisPage.rowEdit(" + meta.row + ");' style='outline:none;text-decoration: none;color:#5091ea;display:none' permission='partner_row_edit'>审核</a>"+
                "<a href='javascript:thisPage.rowInviter(" + meta.row + ");' style='outline:none;text-decoration: none;color:#e750f8;margin-left:4px;display:none' permission='partner_row_inviter' title='绑定邀请人'>邀请人</a>"+
                "<a href='javascript:thisPage.rowReferrer(" + meta.row + ");' style='outline:none;text-decoration: none;color:#fa1640;margin-left:4px;display:none' permission='partner_row_referrer' title='绑定推荐人'>推荐人</a>"; 
                //+ "<a title='分配角色' class='hidden-xs' href='javascript:thisPage.rowAllotRole(" + meta.row + ");' style='outline:none;text-decoration: none;color:#5cb85c;display:none;margin-left:2px;' permission='partner_btn_row_getAllotRole'>角色</a>";
            }
          },
          {
            targets : [1,2,3,4,5,6,7,8],
            render: function(value, type, row, meta){
              return pageDataTable.formatColumn(value);
            }
          },
          {
            visible : false,//列的隐藏显示
            targets : []//指定列
          }
        ]
      });
      pageDataTable.buttons(thisTable);
      pageDataTable.action(thisTable);
      pageDataTable.select(thisTable, tableDom);
      pageDataTable.tooltip();
      var thisJquery = $(tableDom).dataTable();
      var domOwnMenu = 'treeOwnMenu';
      var tree_obj_own = null;
      var settingOwn = {
        view: {
          expandSpeed : 100,
          showLine : true,
          showIcon : true,
          fontCss : {"color":"#000","outline":"none","text-decoration":"none"}
        },
        check: {
          enable: true,
          chkStyle: "checkbox",
          chkboxType: {"Y":"p","N":"s"}
        },
        data: {
          simpleData: {
            enable: true,
            idKey: "kid",
            pIdKey: "pid",
          }
        },
        callback : {
          beforeClick : function(treeId,node,clickFlag){
            return false;
          }
        }
      };
      thisPage = {
        init: function(){
          this.addEvent();
          this.initDom();
        },
        initDom : function(){
          self.chosen_inviter = winFn.initChosen(urlUserInviter,'form_select_inviter',function(value,attrs){});
          self.chosen_referrer = winFn.initChosen(urlUserReferrer,'form_select_referrer',function(value,attrs){});
        },
        createDropdown: function(){
        },
        addEvent: function(){
          this.btnEvent();
        },
        btnEvent: function(){
          $(tableDom + ' tbody').on('dblclick', 'tr', function(){
            if(verifyFn.inputRequired('#partner_row_edit')){
              return;
            }
            thisPage.trDblclick(thisTable.row(this).data());
          });
          $('#btnAdd').on('click', function(){
            thisPage.edit();
          });
          //搜索按钮
          $('#btnSearch').on('click', function(){
            thisPage.search();
          });
          //按钮组-自定义显示列
          $('#iconColumn').parent().parent().on('click', function(){
            pageDataTable.columnCustom(thisTable, tableDom, '#divKeyColumns');
          });
          //按钮组-搜索
          $('#iconRefresh').parent().parent().on('click', function(){
            thisPage.search();
          });
          $('#btnDel').on('click', function(){
            thisPage.delKeys();
          });
          $('#btnAllotRole').on('click',function(){
            if(verifyFn.inputRequired('#partner_btn_row_getAllotRole')){
              layerFn.alert('没有操作权限',AppKey.code.code199);
              return;
            }
            var kids = '';
            $(tableDom + ' tbody input:checked').each(function(){
              var value = this.value;
              if(value != null && value != ''){
                if(kids.length > 0)
                  kids += ",";
                kids += value;
              }
            });
            if(kids == ''){
              layerFn.alert('请选择登录账号',AppKey.code.code199);
              return;
            }
            thisPage.allotRole(null,null,kids);
          });
        },
        allotRole : function(userId,user_name,userIds){
          layerFn.queryGetHint(urlGetAllotRole,{userId:userId},function(data){
            thisPage.roleData(data.data,userId,user_name,userIds);
          });
        },
        roleData : function(data,userId,user_name,userIds){
          winFn.roleData(data,user_name,'#div_role_data',function(title){
            layerFn.addOrEdit(title,'#div_role_data',['500px','430px'],function(index,layero){
              var kids = '';
              $("#div_role_data"+" input[name='roles']:checked").each(function(){
                var value = this.value;
                if(value != null && value != ''){
                  if(kids.length > 0)
                    kids += ",";
                  kids += value;
                }
              });
              if(verifyFn.inputRequired('#partner_btn_row_saveAllotRole')){
                layerFn.alert('没有操作权限',AppKey.code.code199);
                return;
              }
              var params = userIds == null ? {kids:kids,type:1,userId:userId} : {kids:kids,type:2,userIds:userIds};
              layerFn.submit(urlSaveAllotRole,params,function(data){
                thisPage.complete(data,index,true);
              });
            });
          });
        },
        /*分配角色*/
        rowAllotRole : function(index){
          var row = thisJquery.fnGetData(index);
          thisPage.allotRole(row.userId,row.user_name,null);
        },
        rowInviter : function(rowIndex){
          var row = thisTable.row(rowIndex).data();
          if(row.auth_status != 2){
            layerFn.alert('['+row.user_name+']尚未审核通过',AppKey.code.code199);
            return;
          }
          layerFn.addOrEdit('['+row.user_name+']绑定邀请人','#div_chosen_select_inviter',['430px','370px'],function(layerIndex,layero){
            var inviter = self.chosen_inviter.val();
            if(inviter == null){
              layerFn.alert('请选择邀请人',AppKey.code.code199);
              return;
            }
            layerFn.submit(saveUserInviter,{inviter:inviter,userId:row.userId},function(data){
              thisPage.complete(data,layerIndex,true);
            });
          });
          winFn.chosenOption('form_select_inviter','请输入关键字搜索',(row.inviter_id) ? row.inviter_id : null);//赋值input
          winFn.chosenDisplay('form_select_inviter',row.parentUser,row.inviter_id,'请选择邀请人……');//回写text
        },
        rowReferrer : function(rowIndex){
          var row = thisTable.row(rowIndex).data();
          if(row.auth_status != 2){
            layerFn.alert('['+row.user_name+']尚未审核通过',AppKey.code.code199);
            return;
          }
          layerFn.addOrEdit('['+row.user_name+']绑定推荐人','#div_chosen_select_referrer',['430px','370px'],function(layerIndex,layero){
            var referrer = self.chosen_referrer.val();
            if(referrer == null){
              layerFn.alert('请选择推荐人',AppKey.code.code199);
              return;
            }
            layerFn.submit(saveUserReferrer,{kid : row.user_id,inviter:row.userId,userId:referrer},function(data){
              thisPage.complete(data,layerIndex,true);
            });
          });
          winFn.chosenOption('form_select_referrer','请输入关键字搜索',(row.referrer_id) ? row.referrer_id : null);//赋值input
          winFn.chosenDisplay('form_select_referrer',row.referrer_name,row.referrer_id,'请选择推荐人……');//回写text
        },
        /*私有菜单(用户菜单)*/
        rowUserMenu : function(index){
          if(verifyFn.inputRequired('#partner_row_getOwnMenu')){
            layerFn.alert('没有操作权限',AppKey.code.code199);
            return;
          }
          var checkOption = 1;
          var row = thisJquery.fnGetData(index);
          layerFn.queryGetHint(urlGetOwnMenu,{userId:row.userId},function(data){
            tree_obj_own = $.fn.zTree.init($("#"+domOwnMenu),settingOwn,data.data);
            tree_obj_own.expandAll(true);
            layerFn.addOrEdit('账号['+row.user_name+']私有菜单','#div_own_menu',['420px','420px'],function(index){
              var nodes = tree_obj_own.getCheckedNodes(true);
              var kids = '';
              $.each(nodes,function(index,data){
                var value = data.kid;
                if(value != null && value != ''){
                  if(kids.length > 0)
                    kids += ",";
                  kids += value;
                }
              });
              if(verifyFn.inputRequired('#partner_row_saveOwnMenu')){
                layerFn.alert('没有操作权限',AppKey.code.code199);
                return;
              }
              layerFn.submit(urlSaveOwnMenu,{kids:kids,userId:row.userId},function(data){
                layerFn.closeIndex(index);
                layerFn.handleResult(data.msg,data.code);
              });
            },'全选',function(){
              checkOption++;
              if(checkOption % 2 === 0){
                $('a.layui-layer-btn2').text('不选');
                tree_obj_own.checkAllNodes(true);
              }else{
                $('a.layui-layer-btn2').text('全选');
                tree_obj_own.checkAllNodes(false);
              }
            });
          });
        },
        rowProtocol : function(index){
          if(verifyFn.inputRequired('#partner_row_protocol')){
            layerFn.alert('没有操作权限',AppKey.code.code199);
            return;
          }
          var row = thisJquery.fnGetData(index);
          var title = row.protocol == 0 ? '确定要签约如下账号操作？<br/><span style="color:#09b0fc">'+row.user_name+'</span>' : '确定要解约如下账号操作？<br/><span style="color:#09b0fc">'+row.user_name+'</span>';
          layerFn.confirm(title,function(){
            layerFn.submit(urlEditProtocol,{kid:row.userId,target:row.protocol == 0 ? 1 : 0},function(data){
              layerFn.handleResult(data.msg,data.code);
              thisPage.complete(data, null, true);
            });
          });
        },
        trDblclick: function(data){
          thisPage.edit(data.usere_id,data.userId,data.real_name,data.province_id,data.city_id,data.county_id,data.preInvitationUser);
        },
        search: function(){
          if(verifyFn.inputCheck('#partner_btn_listData','没有操作权限')) return;
          $(tableDom + '_wrapper table input[type=checkbox]').prop('checked', false);
          thisTable.draw();
        },
        resetForm: function(){
          winFn.clearFormData('#form_partner_edit');
          winFn.dropdown('#auth_status','','#dropdown_auth_status','认证状态');
        },
        edit: function(kid,user_id,real_name,provinceId,cityId,countyId,preInvitationUser){
          if(verifyFn.inputCheck('#partner_row_edit','没有审核权限')) return;
          var pre_title = '';
          if(preInvitationUser){
            pre_title = '邀请账号['+preInvitationUser+']';
          }
          layerFn.addOrEdit(pre_title+real_name+'-审核','#div_audit',['400px','280px'],function(layerIndex){
            if(verifyFn.inputCheck('#auth_status','请选择审核状态')) return;
            if(verifyFn.inputCheck('#details','审核信息不能为空')) return;
            var params = {
              kid : kid,
              user_id : user_id,
              provinceId : provinceId,
              cityId : cityId,
              countyId : countyId,
              type : 2,
              details : winFn.getDomValue('#details'),
              auth_status : winFn.getDomValue('#auth_status')
            };
            layerFn.submitResult(urlEdit,params,function(data){
              if(AppKey.code.code198 == data.code){
                var json = data.data;
                var _msg = '';
                switch (json.type){
                  case 1:
                    _msg = "("+json.province_name+")省分公司,本次操作不会绑定推荐人,是否继续审核通过";
                    break;
                  case 2:
                    _msg = "("+json.city_name+")市分公司,本次操作会导致("+json.city_name+")市分公司的推荐人解绑,是否继续审核通过";
                    break;
                  case 3:
                    _msg = "("+json.county_name+")区级代理,本次操作会导致("+json.county_name+")区级代理的推荐人解绑,是否继续审核通过";
                    break;
                  default:
                    break;
                }
                layerFn.confirm('事业部用户('+json.user_name+')已邀请了'+_msg,function(){
                  params['confirm'] = 1;
                  layerFn.submit(urlEdit,params,function(data){
                    thisPage.complete(data,layerIndex,true);
                  });
                });
              }else if(AppKey.code.code200 == data.code){
                thisPage.complete(data,layerIndex,true);
              }else{
                layerFn.alert(data.msg,data.code);
              }
            });
          });
          this.resetForm();
        },
        rowEdit: function(index){
          var row = thisJquery.fnGetData(index);
          thisPage.edit(row.user_id,row.userId,row.real_name,row.province_id,row.city_id,row.county_id,row.preInvitationUser);
        },
        complete: function(data, index, search){
          try{
            if(index){
              layerFn.closeIndex(index);
            }
            if(search){
              thisPage.search();
            }
            if(data){
              layerFn.handleResult(data.msg, data.code);
            }
          }catch(e){}
        }
      };
      thisPage.init();
    });
  });
</script>